package com.hssoftvn.android.apps.tinycouplechat;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

public class TinyCoupleChat extends Activity {

	static TinyCoupleChat m_Instance;
	ChatItemApdapter m_ChatItemAdapter;
	ArrayList<ChatItem> m_ListChatItem;
	ListView m_lvChatList;

	ImageView imgAvatar;
	EditText txtTextToSend;
	Button btnEmoticon, btnSend;
	
	ChatUser mME, mYou;
	Drawable mME_avatar=null, mYOU_avatar=null;
	
	public static TinyCoupleChat getInstance()
	{
		return m_Instance;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_chat);
		
		mME  = new ChatUser("Tiny", "", true, Utils.TYPE_ME);
		mYou = new ChatUser("Darling", "", false, Utils.TYPE_YOU); 
		
		m_Instance = this;
		
		m_ListChatItem = new ArrayList<ChatItem>();
		
		m_lvChatList = (ListView)findViewById(R.id.lvMessages);

		m_lvChatList.setOnItemClickListener(new OnItemClickListener()
		   {
		   
		      @Override
		      public void onItemClick(AdapterView<?> adapter, View v, int position,
		            long arg3) 
		      {
		            //ChatItem item = (ChatItem)adapter.getItemAtPosition(position); 
		            // assuming string and if you want to get the value on click of list item
		            // do what you intend to do on click of listview row
		            //Toast.makeText(getApplicationContext(), item.getText(), Toast.LENGTH_SHORT).show();
		      }
		   });		
		
		//tiny: handle title bar event
		m_ChatItemAdapter = new ChatItemApdapter(this, m_ListChatItem);				
		m_lvChatList.setAdapter(m_ChatItemAdapter);
		
		m_ChatItemAdapter.setNotifyOnChange(true);
		
		btnEmoticon = (Button)findViewById(R.id.btnEmoticon);
		btnEmoticon.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Utils.ShowToast("Coming soon!");
				String text = txtTextToSend.getText().toString();
				text += ":)";				
				txtTextToSend.setText(text);
			}
		});
		
		
		btnSend = (Button)findViewById(R.id.btnSend);
		btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub				
				String text =txtTextToSend.getText().toString();
				ChatItem item = 
						new ChatItem( mME_avatar,
									text, Utils.TYPE_ME);
				m_ListChatItem.add(item);
				/*
				ChatItem item2 = 
						new ChatItem( mYOU_avatar,
									text, Utils.TYPE_YOU);
				m_ListChatItem.add(item2);
				*/
				m_ChatItemAdapter.notifyDataSetChanged();
				txtTextToSend.setText("");
				//Send to couplex
				new ChatItemSender().execute(item);
				
				//Check seen status of receiver
				new ChatItemListener().execute();

				//scroll the list to end
				m_lvChatList.smoothScrollToPosition(m_lvChatList.getCount());
			}
		});

		txtTextToSend = (EditText)findViewById(R.id.txtTextToSend);
		txtTextToSend.setHint("Enter message here!");
		
		mME_avatar = Utils.getAvatarByUser(mME);
		mYOU_avatar = Utils.getAvatarByUser(mYou);
		
		//start service
		Intent myIntent = new Intent(this, ServiceCheckingIncomingMSG.class);
	    startService(myIntent);
		
	}
	
	void addCouplexToChatWindow(ChatItem item)
	{

		ChatItem item2 = 
				new ChatItem( mYOU_avatar,
							item.getText(), Utils.TYPE_YOU);
		m_ListChatItem.add(item2);
		m_ChatItemAdapter.notifyDataSetChanged();
	}
	
	void addCouplexToChatWindow(String text)
	{

		ChatItem item2 = 
				new ChatItem( mYOU_avatar,
							text, Utils.TYPE_YOU);
		m_ListChatItem.add(item2);
		m_ChatItemAdapter.notifyDataSetChanged();
	}
	
	void notifyDataChanged()
	{
		m_ChatItemAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tiny_couple_chat, menu);
		return true;
	}
	
	BroadcastReceiver broadcastReceiverCheckingMSG = null;
	
	void registerBroadcastReceivers(){
		if (broadcastReceiverCheckingMSG==null)
		{
			broadcastReceiverCheckingMSG = new BroadcastReceiver() {        	
		        @Override
		        public void onReceive(Context context, Intent intent){
		        	
		        	String text = intent.getExtras().getString(Utils.EXTRA_INCOMING_MSG);
		        	addCouplexToChatWindow(text);
		        }
	        };
		}
        
        IntentFilter progressfilter = new IntentFilter(Utils.INTENT_MSG_RECEIVED );        
        registerReceiver(broadcastReceiverCheckingMSG,progressfilter);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		registerBroadcastReceivers();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(broadcastReceiverCheckingMSG);
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//unregisterReceiver(broadcastReceiverCheckingMSG);
		//stopService(name)
	}
}
